package solucionessgh.TipoCambio;


import java.sql.Connection;

import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import javax.swing.JOptionPane;
import solucionessgh.Util.DataBase;

/**
 *
 * @author roderick
 */
public class TipoCambioModel extends DataBase{
    
    private int id;
    private Date tcbfec;
    private int moncod;
    private double tcbcom;
    private double tcbven;
    
    public TipoCambioModel(){

    }

    private boolean validar(){
        return true;
    }

    public boolean create(){
        if(validar()){
            try{
                Connection con = ConnectionBase();
                PreparedStatement stmt = con.prepareStatement("INSERT INTO tipocambio( " +
                                                              "tcbfec, moncod, tcbcom, tcbven) " +
                                                              "VALUES (?, ?, ?, ?);");
                stmt.setDate(1,(java.sql.Date) this.tcbfec);
                stmt.setInt(2,this.moncod);
                stmt.setDouble(3,this.tcbcom);
                stmt.setDouble(4,this.tcbven);
                stmt.toString();
                stmt.executeUpdate();
                return true;
            }
            catch( Exception e ){
                JOptionPane.showMessageDialog(null,
                    e.getMessage(),
                    "Nivel Usuario",
                    JOptionPane.ERROR_MESSAGE);
            }
        }
        return false;
    }

    public boolean update(){
        if(validar()){
            try{
                Connection con = ConnectionBase();
                PreparedStatement stmt = con.prepareStatement("UPDATE tipocambio SET tcbfec=?, moncod=?, tcbcom=?, tcbven=? WHERE tcid = ? ;");
                stmt.setDate(1, this.tcbfec);
                stmt.setInt(2,this.moncod);
                stmt.setDouble(3,this.tcbcom);
                stmt.setDouble(4,this.tcbven);
                stmt.setInt(5, this.id);
                stmt.executeUpdate();
                return true;
            }
            catch( Exception e ){
                JOptionPane.showMessageDialog(null,
                    e.getMessage(),
                    "Nivel Usuario",
                    JOptionPane.ERROR_MESSAGE);
            }
        }
        return false;
    }

    public boolean delete(){
        if(validar()){
            try{
                Connection con = ConnectionBase();
                PreparedStatement stmt = con.prepareStatement("delete from tipocambio where tcid =?;");
                stmt.setInt(1, this.id);
                stmt.executeUpdate();
                return true;
            }
            catch( Exception e ){
                JOptionPane.showMessageDialog(null,
                    e.getMessage(),
                    "Nivel Usuario",
                    JOptionPane.ERROR_MESSAGE);
            }
        }
        return false;
    }

    public ResultSet read(){
        return this.find("SELECT tp.tcbfec, tp.tcbcom, tp.tcbven,tp.id, m.moncod as moncod FROM tipocambio tp INNER JOIN moneda m ON m.moncod=tp.moncod;");
    }

    public ResultSet finder(String var){
        return this.find("SELECT tc.tcbfec, m.mondesc as moncod, tc.tcbcom, tc.tcbven, tc.tcid "+
                         "FROM tipocambio as tc "+
                         "INNER JOIN moneda as m ON "+
                         "m.moncod=tc.moncod "+
                         "WHERE m.mondesc LIKE '"+var+"%';");
    }

    public ResultSet finder(int var){
        return this.find("SELECT tcbfec, moncod, tcbcom, tcbven, tcid "+
                         "FROM tipocambio WHERE tcid = "+var+";");
    }
    

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

     public int getMoncod() {
        return moncod;
    }

    public void setMoncod(int moncod) {
        this.moncod = moncod;
    }

    public double getTcbcom() {
        return tcbcom;
    }

    public void setTcbcom(double tcbcom) {
        this.tcbcom = tcbcom;
    }

    public Date getTcbfec() {
        return tcbfec;
    }

    public void setTcbfec(Date tcbfec) {
        this.tcbfec = tcbfec;
    }

    public double getTcbven() {
        return tcbven;
    }

    public void setTcbven(double tcbven) {
        this.tcbven = tcbven;
    }
    
}
